package com.chaokang.controller;

import com.chaokang.Entity.CommonUserEntity;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class CommonUserConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${service-url.nacos-user-service}")
    private String commonUserUrl;

    @PostMapping(value = "/getCommonUserByID")

    public CommonUserEntity getCommonUserByID(@RequestBody CommonUserEntity commonUserEntity) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", commonUserEntity.getId());

        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        CommonUserEntity a = restTemplate.postForObject(commonUserUrl + "/service/getCommonUserByID", objectHttpEntity, CommonUserEntity.class);
        return a;
    }

    @PostMapping(value = "/insertCommonUser")
    public int insertCommonUser(@RequestBody CommonUserEntity commonUserEntity) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", commonUserEntity.getUsername());
        map.put("iphone", commonUserEntity.getIphone());
        map.put("password", commonUserEntity.getPassword());
        map.put("rentID", commonUserEntity.getRentID());
        map.put("state", commonUserEntity.getState());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Integer a = restTemplate.postForObject(commonUserUrl + "/service/insertCommonUser", objectHttpEntity, Integer.class);
        return a;


    }

    @PostMapping(value = "/sendCode")
    public Object sendCode(@RequestBody CommonUserEntity commonUserEntity) {
        HashMap<String, Object> map = getMap(commonUserEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Object a=restTemplate.postForObject(commonUserUrl + "/service/sendCode", objectHttpEntity, Object.class);
        return a;


    }

    private HashMap<String, Object> getMap(CommonUserEntity commonUserEntity) {
        HashMap<String, Object> map = new HashMap<>();
        if(commonUserEntity.getId()!=null){
            map.put("id",commonUserEntity.getId());
        }
        if (commonUserEntity.getUsername() != null)
            map.put("username", commonUserEntity.getUsername());
        if (commonUserEntity.getIphone() != null)
            map.put("iphone", commonUserEntity.getIphone());
        if (commonUserEntity.getPassword() != null)
            map.put("password", commonUserEntity.getPassword());
        if (commonUserEntity.getRentID() != null)
            map.put("rentID", commonUserEntity.getRentID());
        if (commonUserEntity.getState() != null)
            map.put("state", commonUserEntity.getState());
        if(commonUserEntity.getVerifyCode() !=null){
            map.put("verifyCode",commonUserEntity.getVerifyCode());
        }
        return map;
    }
    @PostMapping(value = "/changeCommonUserPassword")
    public CommonUserEntity changePassword(@RequestBody CommonUserEntity commonUserEntity){
        HashMap<String,Object> map = getMap(commonUserEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        CommonUserEntity commonUserEntity1 = restTemplate.postForObject(commonUserUrl + "/service/changeCommonUserPassword", objectHttpEntity, CommonUserEntity.class);
        return commonUserEntity1;

    }
    @PostMapping(value = "/sendVerifyCode")
    public Object sendVerifyCode(@RequestBody CommonUserEntity commonUserEntity){
        HashMap<String,Object> map = getMap(commonUserEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Object commonUserEntity1 = restTemplate.postForObject(commonUserUrl + "/service/sendVerifyCode", objectHttpEntity, Object.class);
        return commonUserEntity1;

    }

    @PostMapping(value = "/loginCommonUser")
    public CommonUserEntity login(@RequestBody CommonUserEntity commonUserEntity){
        HashMap<String,Object> map = getMap(commonUserEntity);

        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        CommonUserEntity commonUserEntity1 = restTemplate.postForObject(commonUserUrl + "/service/loginCommonUser", objectHttpEntity, CommonUserEntity.class);

        return commonUserEntity1;
    }
    @PostMapping(value = "/registerCommonUser")
    public HashMap<String, Integer> registerCommonUser(@RequestBody CommonUserEntity commonUserEntity){
        HashMap<String,Object> map = getMap(commonUserEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Integer commonUserEntity1 = restTemplate.postForObject(commonUserUrl + "/service/registerCommonUser", objectHttpEntity, Integer.class);
        HashMap<String,Integer> answer = new HashMap<>();
        answer.put("success",commonUserEntity1);
        return answer;

    }
    @PostMapping(value = "/confirmCommonUserCode")
    public CommonUserEntity loginByVarifyCode(@RequestBody CommonUserEntity commonUserEntity){
        HashMap<String,Object> map = getMap(commonUserEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        CommonUserEntity commonUserEntity1 = restTemplate.postForObject(commonUserUrl + "/service/confirmCommonUserCode", objectHttpEntity, CommonUserEntity.class);
        return commonUserEntity1;
    }




}
